<template>
  <div class="first-loading-wrp">
    <div class="loading-wrp">
      <span class="dot dot-spin">
        <i></i>
        <i></i>
        <i></i>
        <i></i>
      </span>
    </div>
    <h2>身份验证中...</h2>
  </div>
</template>

<script>
  import { api as loginApi } from '/@/api/system/otherApp';
  import { api as wvpApi } from '/@/api/wvp/user.js';
  import { api as userApi } from '/@/api/system/user.js';
  import { localSave } from '/@/utils/local-util.js';
  import LocalStorageKeyConst from '/@/constants/local-storage-key-const.js';
  import { message } from 'ant-design-vue';
  import { buildRoutes } from '/@/router/index';
  import { useUserStore } from '/@/store/modules/system/user';
  import { menuApi } from '/@/api/system/menu-api.js';
  export default {
    data() {
      return {
        formData: {
          grant_type: 'password',
          scope: 'server',
        },
      };
    },
    methods: {
      async login() {
        const res = await loginApi.getJToken();
        const token = `bearer ${res.data}`;
        localSave(LocalStorageKeyConst.USER_TOKEN, token || '');
        localSave(LocalStorageKeyConst.j_TOKEN, 'ab404b3665e20f82027c040f3d939e35');
        // 获取easyLive的token
        // const liveData = await easyLiveApi.getToken();、
        // localSave(LocalStorageKeyConst.live_TOKEN, `Bearer ${liveData.token || ''}`);
        message.success('登录成功');
        // 更新用户信息到pinia
        const userInfo = await userApi.getInfo(this.formData.username || 'admin');
        localSave(LocalStorageKeyConst.USERINFO_TOKEN, userInfo.data ? JSON.stringify(userInfo.data) : '');
        // 构建系统的路由
        const menuTree = await menuApi.getMenu();
        buildRoutes([]);
        // 更新用户信息到pinia
        useUserStore().setMenuList(menuTree.data.filter((m) => m.permission === '0'));
        this.$router.push('/monitor');
        // 获取wvp的token
        loginApi.getWToken().then((res) => {
          if (res.code === 0) {
            localSave(LocalStorageKeyConst.WVP_TOKEN, res.data.accessToken ? res.data.accessToken : '');
          } else {
            message.error('流媒体连接失败');
          }
        });
      },
    },
    mounted() {
      this.login();
    },
  };
</script>

<style type="text/css" scoped>
  .first-loading-wrp {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 90vh;
    min-height: 90vh;
  }

  .first-loading-wrp > h1 {
    font-size: 30px;
    font-weight: bolder;
  }

  .first-loading-wrp .loading-wrp {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 98px;
  }

  .dot {
    position: relative;
    box-sizing: border-box;
    display: inline-block;
    width: 64px;
    height: 64px;
    font-size: 64px;
    transform: rotate(45deg);
    animation: antRotate 1.2s infinite linear;
  }

  .dot i {
    position: absolute;
    display: block;
    width: 28px;
    height: 28px;
    background-color: #1890ff;
    border-radius: 100%;
    opacity: 0.3;
    transform: scale(0.75);
    transform-origin: 50% 50%;
    animation: antSpinMove 1s infinite linear alternate;
  }

  .dot i:nth-child(1) {
    top: 0;
    left: 0;
  }

  .dot i:nth-child(2) {
    top: 0;
    right: 0;
    -webkit-animation-delay: 0.4s;
    animation-delay: 0.4s;
  }

  .dot i:nth-child(3) {
    right: 0;
    bottom: 0;
    -webkit-animation-delay: 0.8s;
    animation-delay: 0.8s;
  }

  .dot i:nth-child(4) {
    bottom: 0;
    left: 0;
    -webkit-animation-delay: 1.2s;
    animation-delay: 1.2s;
  }

  @keyframes antRotate {
    to {
      -webkit-transform: rotate(405deg);
      transform: rotate(405deg);
    }
  }

  @-webkit-keyframes antRotate {
    to {
      -webkit-transform: rotate(405deg);
      transform: rotate(405deg);
    }
  }

  @keyframes antSpinMove {
    to {
      opacity: 1;
    }
  }

  @-webkit-keyframes antSpinMove {
    to {
      opacity: 1;
    }
  }
</style>
